<?php

namespace App\Http\Middleware;

use App\UserV3;
use Closure;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class ApiBase
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        Log::info('api start====');
        $platform = getPlatform($ua = $request->header('user-agent') ? : '');
        $request->merge(['platform'=>$platform,'_with_api'=>true,'_sinfo'=>['start_at'=>time()]]);

        Log::info('request:',$request->all());

        return $next($request);
    }

    public function terminate($request, $response)
    {
        $url = $request->fullUrl();
        $method = $request->getMethod();
        $ip = $request->getClientIp();
        $status = $response->getStatusCode();

        //Log::info('request:',$request->all());
        Log::info("{$ip}: [{$status}] {$method}@{$url}");
        //Log::info('response:',json_decode($response->getContent(),true));
        Log::info('response:',json_decode($response->getContent(),true) ? : []);

    }
}
